import type { VbenFormSchema } from '#/adapter/form';
import type {
  OnActionClickParams,
  VxeTableGridOptions,
} from '#/adapter/vxe-table';
import type { InventoryApi } from '#/models/inventory/inventory';

import { z } from '#/adapter/form';
import { dictFormatter, useDictOptions } from '#/hooks/useDictOptions';
import { $t } from '#/locales';
import { op } from '#/utils/permission';

/**
 * 搜索表单配置
 * @description 用于仓库列表页的搜索表单字段配置
 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'warehouse_name',
      label: $t('inventory.warehouse.name'),
      componentProps: {
        placeholder: $t('inventory.warehouse.name'),
        allowClear: true,
      },
    },
    {
      component: 'Input',
      fieldName: 'warehouse_code',
      label: $t('inventory.warehouse.code'),
      componentProps: {
        placeholder: $t('inventory.warehouse.code'),
        allowClear: true,
      },
    },
    {
      component: 'Select',
      fieldName: 'status',
      label: $t('common.other.status'),
      componentProps: {
        placeholder: $t('common.other.status'),
        allowClear: true,
        options: useDictOptions('inventory_wh_status'),
      },
    },
  ];
}

// region 选项卡分类
/**
 * 获取按选项卡分类的仓库表单配置
 * @description 为仓库创建和编辑表单提供选项卡分类结构支持
 */
export interface TabbedSchemaItem {
  key: string;
  tab: string;
  fields: string[];
}

export function useTabbedSchema(): TabbedSchemaItem[] {
  return [
    {
      key: 'basic',
      tab: $t('inventory.warehouse.tabBasicInfo'),
      fields: [
        'warehouse_code',
        'warehouse_name',
        'status',
        'is_primary',
        'description',
      ],
    },
    {
      key: 'location',
      tab: $t('inventory.warehouse.tabLocationInfo'),
      fields: ['address', 'capacity', 'area'],
    },
    {
      key: 'contact',
      tab: $t('inventory.warehouse.tabContactInfo'),
      fields: ['contact_person', 'contact_phone', 'manager_id'],
    },
    {
      key: 'advanced',
      tab: $t('inventory.warehouse.tabAdvanced'),
      fields: ['remark'],
    },
  ];
}

/**
 * 创建和编辑表单配置
 * @description 用于仓库创建和编辑表单的字段配置
 */
export function useSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'warehouse_code',
      label: $t('inventory.warehouse.code'),
      rules: z
        .string()
        .min(1, $t('ui.formRules.required', [$t('inventory.warehouse.code')]))
        .max(
          50,
          $t('ui.formRules.maxLength', [$t('inventory.warehouse.code'), 50]),
        ),
      componentProps: {
        placeholder: $t('inventory.warehouse.code'),
        class: 'w-1/2',
      },
    },
    {
      component: 'Input',
      fieldName: 'warehouse_name',
      label: $t('inventory.warehouse.name'),
      rules: z
        .string()
        .min(1, $t('ui.formRules.required', [$t('inventory.warehouse.name')]))
        .max(
          100,
          $t('ui.formRules.maxLength', [$t('inventory.warehouse.name'), 100]),
        ),
      componentProps: {
        placeholder: $t('inventory.warehouse.name'),
        class: 'w-full',
      },
    },
    {
      component: 'Textarea',
      fieldName: 'description',
      label: $t('inventory.warehouse.description'),
      rules: z.string().optional(),
      componentProps: {
        placeholder: $t('inventory.warehouse.description'),
        rows: 3,
      },
    },
    {
      component: 'Textarea',
      fieldName: 'address',
      label: $t('inventory.warehouse.address'),
      rules: z
        .string()
        .optional()
        .refine((val) => !val || val.length <= 255, {
          message: $t('ui.formRules.maxLength', [
            $t('inventory.warehouse.address'),
            255,
          ]),
        }),
      componentProps: {
        placeholder: $t('inventory.warehouse.address'),
        rows: 2,
      },
    },
    {
      component: 'InputNumber',
      fieldName: 'capacity',
      label: $t('inventory.warehouse.capacity'),
      rules: z
        .number()
        .min(
          0,
          $t('ui.formRules.minLength', [$t('inventory.warehouse.capacity'), 0]),
        )
        .optional(),
      componentProps: {
        placeholder: $t('inventory.warehouse.capacity'),
        min: 0,
        style: { width: '100%' },
      },
    },
    {
      component: 'InputNumber',
      fieldName: 'area',
      label: $t('inventory.warehouse.area'),
      rules: z
        .number()
        .min(
          0,
          $t('ui.formRules.minLength', [$t('inventory.warehouse.area'), 0]),
        )
        .optional(),
      componentProps: {
        placeholder: $t('inventory.warehouse.area'),
        min: 0,
        precision: 2,
        style: { width: '100%' },
      },
    },
    {
      component: 'Input',
      fieldName: 'contact_person',
      label: $t('inventory.warehouse.contact_person'),
      rules: z
        .string()
        .optional()
        .refine((val) => !val || val.length <= 50, {
          message: $t('ui.formRules.maxLength', [
            $t('inventory.warehouse.contact_person'),
            50,
          ]),
        }),
      componentProps: {
        placeholder: $t('inventory.warehouse.contact_person'),
        class: 'w-1/2',
      },
    },
    {
      component: 'Input',
      fieldName: 'contact_phone',
      label: $t('inventory.warehouse.contact_phone'),
      rules: z
        .string()
        .optional()
        .refine((val) => !val || val.length <= 20, {
          message: $t('ui.formRules.maxLength', [
            $t('inventory.warehouse.contact_phone'),
            20,
          ]),
        }),
      componentProps: {
        placeholder: $t('inventory.warehouse.contact_phone'),
        class: 'w-1/2',
      },
    },
    {
      component: 'InputNumber',
      fieldName: 'manager_id',
      label: $t('inventory.warehouse.manager_id'),
      rules: z
        .number()
        .min(
          0,
          $t('ui.formRules.minLength', [
            $t('inventory.warehouse.manager_id'),
            0,
          ]),
        )
        .optional(),
      componentProps: {
        placeholder: $t('inventory.warehouse.manager_id'),
        min: 0,
        style: { width: '100%' },
      },
    },
    {
      component: 'Select',
      fieldName: 'status',
      label: $t('common.other.status'),
      rules: z
        .string()
        .min(1, $t('ui.formRules.required', [$t('common.other.status')])),
      defaultValue: 'active',
      componentProps: {
        placeholder: $t('common.other.status'),
        options: [
          { label: '激活', value: 'active' },
          { label: '非激活', value: 'inactive' },
          { label: '已关闭', value: 'closed' },
        ],
        class: 'w-1/2',
      },
    },
    {
      component: 'Checkbox',
      fieldName: 'is_primary',
      label: $t('inventory.warehouse.is_primary'),
      rules: z.boolean().optional(),
      componentProps: {},
    },
    {
      component: 'Textarea',
      fieldName: 'remark',
      label: $t('common.db_generalField.remark'),
      rules: z
        .string()
        .optional()
        .refine((val) => !val || val.length <= 256, {
          message: $t('ui.formRules.maxLength', [
            $t('common.db_generalField.remark'),
            256,
          ]),
        }),
      componentProps: {
        placeholder: $t('common.db_generalField.remark'),
        rows: 3,
        maxLength: 256,
        showCount: true,
      },
    },
  ];
}

/**
 * 表格列配置
 * @description 使用函数的形式返回列数据而不是直接export一个Array常量，是为了响应语言切换时重新翻译表头
 * @param onActionClick 表格操作按钮点击事件
 */
export function useColumns(
  onActionClick?: (params: OnActionClickParams<InventoryApi.Warehouse>) => void,
): VxeTableGridOptions<InventoryApi.Warehouse>['columns'] {
  return [
    {
      title: '序',
      type: 'seq',
      width: 60,
      align: 'center',
      visible: true,
    },
    {
      field: 'warehouse_code',
      title: $t('inventory.warehouse.code'),
      width: 120,
      align: 'left',
      showOverflow: 'tooltip',
      visible: true,
    },
    {
      field: 'warehouse_name',
      title: $t('inventory.warehouse.name'),
      width: 180,
      align: 'left',
      showOverflow: 'tooltip',
      visible: true,
    },
    {
      field: 'description',
      title: $t('inventory.warehouse.description'),
      minWidth: 150,
      align: 'left',
      showOverflow: 'tooltip',
      visible: true,
    },
    {
      field: 'address',
      title: $t('inventory.warehouse.address'),
      minWidth: 200,
      align: 'left',
      showOverflow: 'tooltip',
      visible: true,
    },
    {
      field: 'contact_person',
      title: $t('inventory.warehouse.contact_person'),
      width: 100,
      align: 'left',
      showOverflow: 'tooltip',
      visible: true,
    },
    {
      field: 'contact_phone',
      title: $t('inventory.warehouse.contact_phone'),
      width: 120,
      align: 'left',
      showOverflow: 'tooltip',
      visible: true,
    },
    {
      field: 'manager_id',
      title: $t('inventory.warehouse.manager_id'),
      width: 100,
      align: 'center',
      showOverflow: 'tooltip',
      visible: true,
    },
    {
      field: 'status',
      title: $t('common.other.status'),
      // 使用简单的格式化函数确保数据正确显示
      formatter: dictFormatter('inventory_wh_status'),
      width: 100,
      align: 'center',
      showOverflow: 'tooltip',
      visible: true,
    },
    {
      field: 'is_primary',
      title: $t('inventory.warehouse.is_primary'),
      // 直接内联布尔值格式化逻辑，确保正确处理
      formatter: (row: any) => {
        return row ? (row.is_primary ? '是' : '否') : '';
      },
      width: 100,
      align: 'center',
      showOverflow: 'tooltip',
      visible: true,
    },
    {
      field: 'capacity',
      title: $t('inventory.warehouse.capacity'),
      width: 100,
      align: 'center',
      showOverflow: 'tooltip',
      visible: true,
    },
    {
      field: 'area',
      title: $t('inventory.warehouse.area'),
      width: 100,
      align: 'center',
      showOverflow: 'tooltip',
      visible: true,
    },
    {
      field: 'create_time',
      title: $t('common.db_generalField.create_time'),
      width: 150,
      align: 'left',
      showOverflow: 'tooltip',
      visible: true,
    },
    {
      field: 'update_time',
      title: $t('common.db_generalField.update_time'),
      width: 150,
      align: 'left',
      showOverflow: 'tooltip',
      visible: true,
    },
    {
      align: 'center',
      cellRender: {
        attrs: {
          nameField: 'warehouse_name',
          nameTitle: $t('inventory.warehouse.title'),
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: [
          op('inventory:warehouse:edit', 'edit'),
          op('inventory:warehouse:delete', 'delete'),
        ],
      },
      field: 'action',
      fixed: 'right',
      title: '操作',
      width: 120,
      visible: true,
    },
  ];
}
